/*
 * Copyright (c) 2015  3 7
 * ---2 :19:20
 * ---作者:吴培基
 * ---QQ:757671834
 */

package kali.wpj.kali.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gitonway.lee.niftymodaldialogeffects.lib.Effectstype;
import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;

import butterknife.ButterKnife;
import butterknife.InjectView;
import kali.wpj.kali.MainActivity;
import kali.wpj.kali.R;
import kali.wpj.kali.util.IPTools;
import kali.wpj.kali.util.SharedTools;

public class settingActivity extends ActionBarActivity {
    @InjectView(R.id.ed_common_ip)
     EditText ed_common_ip;
    @InjectView(R.id.btn_common_sure)
     Button btn_common_sure;
    @InjectView(R.id.btn_common_cancle)
//     Button btn_common_cancle;
//    @InjectView(R.id.ed_search_ip)
//    EditText ed_search_ip;
//    @InjectView(R.id.btn_whois_ip)
//    Button btn_whois_ip;
//    @InjectView(R.id.txt_whois_result)
    TextView txt_whois_result;
    private String IP;//whois模块ip（数字）
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {

            txt_whois_result.setText("IP:"+msg.obj.toString());
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.inject(this);
        btn_common_sure.setOnClickListener(btnOnclickerListener);
//        btn_common_cancle.setOnClickListener(btnOnclickerListener);
//        btn_whois_ip.setOnClickListener(btnOnclickerListener);
    }
    View.OnClickListener btnOnclickerListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_common_sure :
                    boolean isIP=IPTools.isIPAddress(ed_common_ip.getText().toString().trim());
                    if(!isIP){
                        dialogShow("输入有误");
                        ed_common_ip.setText("");
                        return ;
                    }
                    Log.i("iSIP",isIP+"-----"+ed_common_ip.getText().toString());
                  boolean result=  writeIpToPhone(settingActivity.this,"ip",ed_common_ip.getText().toString());
                                if(result){
                                    Toast.makeText(settingActivity.this,"设置成功",Toast.LENGTH_SHORT).show();
                                    returnMain(settingActivity.this, MainActivity.class);
                                }else{
                                    dialogShow("失败");
                                }
                                    break;
                    case R.id.btn_common_cancle :
                    returnMain(settingActivity.this, MainActivity.class);
//                    break;case  R.id.btn_whois_ip:
                    new Thread(runnable).start();
            }

        }
    };

    private boolean writeIpToPhone(Context context,String key,String value) {
      boolean result=  SharedTools.putString(context,key,value);
        return result;
    }
    private void returnMain(Context context,Class clazz){
        Intent intent=new Intent(context,clazz);
        startActivity(intent);
        overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_in_top);
        settingActivity.this.finish();
    }

Runnable runnable=new Runnable() {
   String tem=null;
    @Override
    public void run() {
     //   tem=IPTools.getIPByHostName(ed_search_ip.getText().toString());
//        System.out.println("Setting:"+tem);
//       Message msg=new Message();
//        msg.obj=tem;
//        handler.sendMessage(msg);
    }

};
    public void dialogShow(String str){
        NiftyDialogBuilder dialogBuilder=NiftyDialogBuilder.getInstance(settingActivity.this);
        dialogBuilder
                .withTitle("提醒")                                  //.withTitle(null)  no title
                .withTitleColor("#FFFFFF")                                  //def
                .withDividerColor("#11000000")                              //def
                .withMessage(str)                     //.withMessage(null)  no Msg
                .withMessageColor("#FFFFFFFF")                              //def  | withMessageColor(int resid)
                .withDialogColor(getString(R.string.black))                               //def  | withDialogColor(int resid)                               //def
                .isCancelableOnTouchOutside(true)                           //def    | isCancelable(true)
                .withDuration(700)                                          //def
                .withEffect(Effectstype.RotateBottom)
                .withIcon(getResources().getDrawable(R.drawable.warn))//def Effectstype.Slidetop
                .setCustomView(R.layout.custom_view,this)         //.setCustomView(View or ResId,context)
                .show();

    }

}
